/**
 * Copyright &copy; 2012-2016 <a href="https://istep.lczyfz.com">Istep</a> All rights reserved.
 */
package com.lczyfz.istep.modules.race.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfz.istep.modules.race.entity.CtfSetSubject;
import com.lczyfz.istep.modules.race.entity.RaceSubject;
import com.lczyfz.istep.modules.resource.entity.ResourceCtfSet;
import com.lczyfz.istep.modules.resource.entity.ResourceCtfSubject;
import com.lczyfz.istep.modules.resource.entity.ResourceCtfSubjectType;
import com.lczyfz.istep.modules.resource.service.ResourceCtfSubjectService;
import com.lczyfz.istep.modules.resource.service.ResourceCtfSubjectTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfz.istep.common.config.Global;
import com.lczyfz.istep.common.persistence.Page;
import com.lczyfz.istep.common.web.BaseController;
import com.lczyfz.istep.common.utils.StringUtils;
import com.lczyfz.istep.modules.race.entity.RaceRepertory;
import com.lczyfz.istep.modules.race.service.RaceRepertoryService;

import java.util.ArrayList;
import java.util.List;

/**
 * 竞赛题库管理Controller
 * @author CY
 * @version 2017-04-24
 */
@Controller
@RequestMapping(value = "${adminPath}/race/raceRepertory")
public class RaceRepertoryController extends BaseController {
	@Autowired
	private RaceRepertoryService raceRepertoryService;

   @Autowired
   private ResourceCtfSubjectService resourceCtfSubjectService;

	@Autowired
	 private ResourceCtfSubjectTypeService resourceCtfSubjectTypeService;

	@ModelAttribute
	 public RaceRepertory get(@RequestParam(required=false) String id) {
		RaceRepertory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = raceRepertoryService.get(id);
		}
		if (entity == null){
			entity = new RaceRepertory();
		}
		return entity;
	}

	@RequiresPermissions("race:raceRepertory:view")
	@RequestMapping(value = {"list", ""})
	public String list(RaceRepertory raceRepertory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RaceRepertory> page = raceRepertoryService.findPage(new Page<RaceRepertory>(request, response), raceRepertory); 
		model.addAttribute("page", page);
		return "modules/race/raceRepertoryList";
	}

	@RequiresPermissions("race:raceRepertory:view")
	@RequestMapping(value = "form")
	public String form(RaceRepertory raceRepertory, Model model) {
		model.addAttribute("raceRepertory", raceRepertory);
       List<ResourceCtfSet> resourceCtfSets = raceRepertoryService.resourceCtfSets();
       model.addAttribute("resourceCtfSet",resourceCtfSets);
		return "modules/race/raceRepertoryForm";
	}

	@RequiresPermissions("race:raceRepertory:edit")
	@RequestMapping(value = "save")
	public String save(RaceRepertory raceRepertory, Model model, RedirectAttributes redirectAttributes,RaceSubject raceSubject,String duallist,String copy) {
		if (!beanValidator(model, raceRepertory)){
			return form(raceRepertory, model);
		}

		if (copy == null || copy.equals("")) {
				RaceRepertory raceRepertory1 = raceRepertoryService.findRepertoryName(raceRepertory.getRepertoryName());
				if (null != raceRepertory1) {
					addMessage(redirectAttributes, "该竞赛题库已存在，添加失败");
					return "redirect:"+Global.getAdminPath()+"/race/raceRepertory/?repage";
				} else {
					raceRepertoryService.save(raceRepertory);
			}
				//拿到题目集ID
				String ctfSetId = "";
				List<ResourceCtfSet> resourceCtfSets = raceRepertoryService.resourceCtfSets();
				for (ResourceCtfSet resourceCtfSet1 : resourceCtfSets) {
					if (resourceCtfSet1.getCtfSetName().equals(raceRepertory.getCftsetId())) {
						ctfSetId = resourceCtfSet1.getId();
					}
				}
				//拿到对应题目集里面的题目
				List<CtfSetSubject> ctfSetSubjects = raceRepertoryService.findListCtfSetSubject(ctfSetId);
				List<ResourceCtfSubject> ctfSubjects = new ArrayList<ResourceCtfSubject>();
				for (CtfSetSubject ctfSetSubject : ctfSetSubjects) {
					ResourceCtfSubject resourceCtfSubject = raceRepertoryService.findCtfSubject(ctfSetSubject.getCtfSubjectId());
					raceSubject.setId(resourceCtfSubject.getId());
					raceSubject.setRepertoryId(raceRepertory.getId());
					raceSubject.setCtfSubjectName(resourceCtfSubject.getCtfSubjectName());
					raceSubject.setCtfSubjectAttribute(resourceCtfSubject.getCtfSubjectAttribute());
					raceSubject.setLabel(resourceCtfSubject.getLabel());
					raceSubject.setDifficulty(resourceCtfSubject.getQuestionDifficulty());
					raceSubject.setStem(resourceCtfSubject.getStem());
					raceSubject.setType(resourceCtfSubject.getCtfSubjectTypeId());
					raceSubject.setAttachment(resourceCtfSubject.getAttachment());
					raceSubject.setScore(resourceCtfSubject.getScore());
					raceSubject.setCtfEnvironmentId(resourceCtfSubject.getCtfEnvironmentId());
					raceSubject.setAnswer(resourceCtfSubject.getAnswer());
					raceSubject.setCtfEnvironmentName(resourceCtfSubject.getCtfEnvironmentName());
					raceSubject.setTip(resourceCtfSubject.getTip());
					raceSubject.setSolution(resourceCtfSubject.getSolution());
					raceSubject.setLink(resourceCtfSubject.getLink());
					raceRepertoryService.insertRaceSubject(raceSubject);
				}
				addMessage(redirectAttributes, "保存竞赛题库成功");
		}
		else if(copy.equals("no")){
			if (duallist != null && !duallist.equals("")&&!raceRepertory.getRepertoryName().equals("")) {
				raceRepertoryService.save(raceRepertory);
				raceRepertoryService.deleteCtf(raceRepertory.getId());
				String[] strs = duallist.split(",");
				List<ResourceCtfSubject> resourceCtfSubjects = raceRepertoryService.findAddCtfSubject();
				for (int i = 0, len = strs.length; i < len; i++) {
					for (int j = 0; j < resourceCtfSubjects.size(); j++) {
						if (strs[i].toString().equals(resourceCtfSubjects.get(j).getCtfSubjectName())) {
							raceSubject.setId(resourceCtfSubjects.get(j).getId());
							raceSubject.setRepertoryId(raceRepertory.getId());
							raceSubject.setCtfSubjectName(resourceCtfSubjects.get(j).getCtfSubjectName());
							raceSubject.setCtfSubjectAttribute(resourceCtfSubjects.get(j).getCtfSubjectAttribute());
							raceSubject.setLabel(resourceCtfSubjects.get(j).getLabel());
							raceSubject.setDifficulty(resourceCtfSubjects.get(j).getQuestionDifficulty());
							raceSubject.setStem(resourceCtfSubjects.get(j).getStem());
							raceSubject.setType(resourceCtfSubjects.get(j).getCtfSubjectTypeId());
							raceSubject.setAttachment(resourceCtfSubjects.get(j).getAttachment());
							raceSubject.setScore(resourceCtfSubjects.get(j).getScore());
							raceSubject.setCtfEnvironmentId(resourceCtfSubjects.get(j).getCtfEnvironmentId());
							raceSubject.setAnswer(resourceCtfSubjects.get(j).getAnswer());
							raceSubject.setCtfEnvironmentName(resourceCtfSubjects.get(j).getCtfEnvironmentName());
							raceSubject.setTip(resourceCtfSubjects.get(j).getTip());
							raceSubject.setSolution(resourceCtfSubjects.get(j).getSolution());
							raceSubject.setLink(resourceCtfSubjects.get(j).getLink());
							raceRepertoryService.insertRaceSubject(raceSubject);
							addMessage(redirectAttributes, "修改题库成功");
						}
					}
				}
			}else if (raceRepertory.getRepertoryName().equals("")){
//				raceRepertoryService.deleteCtf(raceRepertory.getId());
				addMessage(redirectAttributes, "题库名不能为空，编辑失败");
				return "redirect:"+Global.getAdminPath()+"/race/raceRepertory/?repage";
			}else {
				addMessage(redirectAttributes, "题库内题目不能为空，编辑失败");
				return "redirect:"+Global.getAdminPath()+"/race/raceRepertory/?repage";
			}
		} else if (copy.equals("copy")){
			if (duallist != null && !duallist.equals("")&&!raceRepertory.getRepertoryName().equals("")) {
			RaceRepertory raceRepertory1 = new RaceRepertory();
			raceRepertory1.setRepertoryName(raceRepertory.getRepertoryName());
			raceRepertory1.setRepertoryDetail(raceRepertory.getRepertoryDetail());
			raceRepertoryService.save(raceRepertory1);
			String[] strs = duallist.split(",");
			List<ResourceCtfSubject> resourceCtfSubjects = raceRepertoryService.findAddCtfSubject();
			for(int i=0,len=strs.length;i<len;i++) {
				for (int j = 0; j < resourceCtfSubjects.size(); j++) {
					if (strs[i].toString().equals(resourceCtfSubjects.get(j).getCtfSubjectName())) {
						raceSubject.setId(resourceCtfSubjects.get(j).getId());
						raceSubject.setRepertoryId(raceRepertory1.getId());
						raceSubject.setCtfSubjectName(resourceCtfSubjects.get(j).getCtfSubjectName());
						raceSubject.setCtfSubjectAttribute(resourceCtfSubjects.get(j).getCtfSubjectAttribute());
						raceSubject.setLabel(resourceCtfSubjects.get(j).getLabel());
						raceSubject.setDifficulty(resourceCtfSubjects.get(j).getQuestionDifficulty());
						raceSubject.setStem(resourceCtfSubjects.get(j).getStem());
						raceSubject.setType(resourceCtfSubjects.get(j).getCtfSubjectTypeId());
						raceSubject.setAttachment(resourceCtfSubjects.get(j).getAttachment());
						raceSubject.setScore(resourceCtfSubjects.get(j).getScore());
						raceSubject.setCtfEnvironmentId(resourceCtfSubjects.get(j).getCtfEnvironmentId());
						raceSubject.setAnswer(resourceCtfSubjects.get(j).getAnswer());
						raceSubject.setCtfEnvironmentName(resourceCtfSubjects.get(j).getCtfEnvironmentName());
						raceSubject.setTip(resourceCtfSubjects.get(j).getTip());
						raceSubject.setSolution(resourceCtfSubjects.get(j).getSolution());
						raceSubject.setLink(resourceCtfSubjects.get(j).getLink());
						raceRepertoryService.insertRaceSubject(raceSubject);
						addMessage(redirectAttributes, "复制题库成功");
					}
				}
			}
			}else if (raceRepertory.getRepertoryName().equals("")){
//				RaceRepertory raceRepertory1 = new RaceRepertory();
//				raceRepertory1.setRepertoryName(raceRepertory.getRepertoryName());
//				raceRepertory1.setRepertoryDetail(raceRepertory.getRepertoryDetail());
//				raceRepertoryService.save(raceRepertory1);
				addMessage(redirectAttributes, "该题库名为空，复制失败");

			}else{
				addMessage(redirectAttributes, "该题库题目为空，复制失败");
			}
		}
		return "redirect:"+Global.getAdminPath()+"/race/raceRepertory/?repage";
	}


	@RequiresPermissions("race:raceRepertory:view")
	@RequestMapping(value = "editor")
	public String editor(RaceRepertory raceRepertory, Model model, String type) {
		model.addAttribute("copy", "no");
		List<RaceSubject> raceSubjects =  raceRepertoryService.findRaceSubject();
		List<RaceSubject> raceSubject = new ArrayList<RaceSubject>();

		for (RaceSubject raceSubject1 : raceSubjects){
			if (raceSubject1.getRepertoryId().equals(raceRepertory.getId())){
				raceSubject.add(raceSubject1);
			}
		}

		List<ResourceCtfSubject> resourceCtfSubjectss = raceRepertoryService.findAddCtfSubject();
		List<ResourceCtfSubject> resourceCtfSubjects = new ArrayList<ResourceCtfSubject>();
        int num=0;
		for (int i = 0; i < resourceCtfSubjectss.size(); i++){
			for (int j = 0; j < raceSubject.size(); j++){
						if(resourceCtfSubjectss.get(i).getId().equals(raceSubject.get(j).getId())){
						}else {
							num++;
						}
				         if (num == raceSubject.size()){
							 resourceCtfSubjects.add(resourceCtfSubjectss.get(i));
						 }
			   }
			num=0;
		}
		if(type!=null&&type.equals("copy"))
		{
			raceRepertory.setRepertoryName(raceRepertory.getRepertoryName() + "-副本");
			model.addAttribute("copy", "copy");
		}
		model.addAttribute("raceRepertory", raceRepertory);

		if (raceSubject == null || raceSubject.size() == 0){
			model.addAttribute("resourceCtfSubjects", resourceCtfSubjectss);
		}else {
			model.addAttribute("resourceCtfSubjects", resourceCtfSubjects);
		}
       	model.addAttribute("raceSubject", raceSubject);

		return  "modules/race/raceRepertoryEditor";
	}


	//竞赛题库查看详情
	@RequiresPermissions("race:raceRepertory:edit")
	@RequestMapping(value = "Detail")
	public String Detail(RaceRepertory raceRepertory, Model model) {
		//竞赛题目
		  List<RaceSubject> raceSubjects =  raceRepertoryService.findRaceSubject();
		  List<RaceSubject> raceSubject = new ArrayList<RaceSubject>();
		for (RaceSubject raceSubject1 : raceSubjects){
			if (raceSubject1.getRepertoryId().equals(raceRepertory.getId())){
				raceSubject.add(raceSubject1);
			}
		}
		  model.addAttribute("raceSubject", raceSubject);

		List<ResourceCtfSubjectType> ctfSubjectTypeList=resourceCtfSubjectTypeService.findList(new ResourceCtfSubjectType());
		model.addAttribute("ctfSubjectTypeList", ctfSubjectTypeList);

//		for (ResourceCtfSubjectType resourceCtfSubjectType : ctfSubjectTypeList){
//			System.out.println("--" + resourceCtfSubjectType.getSubjectTypeName());
//		}
		return "modules/race/raceRepertoryDetail";

	}

	@RequiresPermissions("race:raceRepertory:edit")
	@RequestMapping(value = "delete")
	public String delete(RaceRepertory raceRepertory, RedirectAttributes redirectAttributes) {
		raceRepertoryService.delete(raceRepertory);
		raceRepertoryService.deleteCtf(raceRepertory.getId());
		addMessage(redirectAttributes, "删除竞赛题库成功");
		return "redirect:"+Global.getAdminPath()+"/race/raceRepertory/?repage";
	}

}